# Replication File: Manuscript PSRM-OA-2015-0037
# Wojcik, Stefan: Why Legislative Networks? Analyzing Legislative Network Formation
# Oct 24, 2016
#install.packages("ergm")
#install.packages("latticeExtra")
#install.packages("stargazer")

#### MODELS THAT USE IMPUTATION: (TABLE 3)
library(ergm)
library(stargazer)
library(methods)
#library(doMC)
#registerDoMC(28) #use parallel processing if applicaable
load("PSRM_replication_image_networks_IMP.RData")
control.min = control.ergm(MCMC.burnin=1000000, MCMC.interval=200, MCMC.samplesize=300000, MCMLE.maxit=100) 
#control.min = control.ergm(MCMC.burnin=10000, MCMC.interval=50, MCMC.samplesize=20000, MCMLE.maxit=30)

sink("PSRM_replication_logERGM_tables3.txt")
print("Models that use imputation: (TABLE 3)")
#communication
set.seed(12)
fit1 <- ergm(netcom~edges+ absdiff("age") +nodematch("education", diff=F)+nodematch("floor", diff=F) + nodematch("leadership", diff=T) + nodematch("UF", diff=F) +nodematch("party", diff=F)+ transitiveties() , estimate="MLE", control=control.min)

#social
set.seed(12)
fit2 <- ergm(netsoc~edges+ absdiff("age") +nodematch("education", diff=F)+nodematch("floor", diff=F) + nodematch("leadership", diff=T) + nodematch("UF", diff=F) +nodematch("party", diff=F)+ transitiveties() , estimate="MLE", control=control.min)

#information
set.seed(12)
fit3 <- ergm(netinfo~edges+ absdiff("age") +nodematch("education", diff=F)+nodematch("floor", diff=F) + nodematch("leadership", diff=T) + nodematch("UF", diff=F) +nodematch("party", diff=F)+ transitiveties() , estimate="MLE", control=control.min)
stargazer(fit1, fit2, fit3, title = "TABLE 3", out="results_table_3_only.txt")

sink()
